package cn.cloud.all.security.oauth2.common.util;

import org.springframework.core.ConfigurableObjectInputStream;

import java.io.*;

public class SerializationUtils {

    public static byte[] serialize(Object state) {
        ObjectOutputStream oos = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream(512);
            oos = new ObjectOutputStream(bos);
            oos.writeObject(state);
            oos.flush();
            return bos.toByteArray();
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    // eat it
                }
            }
        }
    }

    public static <T> T deserialize(byte[] byteArray) {
		try (ObjectInputStream oip = new ConfigurableObjectInputStream(new ByteArrayInputStream(byteArray), Thread.currentThread().getContextClassLoader())) {
			@SuppressWarnings("unchecked")
			T result = (T) oip.readObject();
			return result;
		} catch (IOException | ClassNotFoundException e) {
			throw new IllegalArgumentException(e);
		}
	}
}
